<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['metaverse-users:users:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['metaverse-users:users:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['metaverse-users:users:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['metaverse-users:users:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="usersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="nickName" />
      <el-table-column label="头像" align="center" prop="avatarUrl">
        <template slot-scope="scope">
          <el-avatar shape="square" :src="scope.row.avatarUrl" :size="50"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="金币" align="center" prop="gold" />
      <el-table-column label="积分" align="center" prop="integral" />
      <el-table-column label="等级" align="center" prop="level" />
      <el-table-column label="经验" align="center" prop="exp" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            @click="sel(scope.row)"
            type="text"
            size="mini"
            icon="el-icon-view"
            v-hasPermi="['metaverse-users:users:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['metaverse-users:users:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['metaverse-users:users:remove']"
          >删除</el-button>
        </template>

      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情 -->
    <el-drawer
      title="用户详情"
      :visible.sync="drawer"
      direction="rtl"
      size="700px">
      <div class="text_style">
        <tr>
          <td>id：{{form.id}}</td>
          <td><div style="margin-left:60px;">小程序中的唯一ID：{{form.openId}}</div></td>
        </tr>
        <tr>
          <td>名称：{{form.nickName}}</td>
          <td><div style="margin-left:60px;">性别：{{["男", "女", "未知"][form.gender]}}</div></td>
        </tr>
        <tr>
          <td>
            头像：<el-image :src="form.avatarUrl" style="width: 30px; height: 30px;"></el-image>
          </td>
          <td><div style="margin-left:60px;">城市：{{form.city}}</div></td>
        </tr>
        <tr>
          <td>语言：{{form.language}}</td>
          <td><div style="margin-left:60px;">省份：{{form.province}}</div></td>
        </tr>
        <tr>
          <td>国家：{{form.country}}</td>
          <td><div style="margin-left:60px;">金币：{{form.gold}}</div></td>
        </tr>
        <tr>
          <td>积分：{{form.integral}}</td>
          <td><div style="margin-left:60px;">等级：{{form.level}}</div></td>
        </tr>
        <tr>
          <td>经验：{{form.exp}}</td>
          <td><div style="margin-left:60px;">水印id：{{form.wId}}</div></td>

        </tr>
        <tr>
          <td>注册ip：{{form.registerIp}}</td>
          <td><div style="margin-left:60px;">注册时间：{{form.registerTime}}</div></td>
        </tr>
        <tr>
          <td>登录时间：{{form.loginTime}}</td>
          <td><div style="margin-left:60px;">登录ip：{{form.loginIp}}</div></td>
        </tr>
        <tr>
          <td>是否删除：{{["未删除","已删除"][form.isDel]}}</td>
          <td><div style="margin-left:60px;">状态：{{["正常","禁用"][form.status]}}</div></td>
        </tr>
        <tr>
          <td>更新者：{{form.updateBy}}</td>
        </tr>
      </div>

    </el-drawer>

    <!-- 添加或修改用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="小程序中的唯一ID" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入小程序中的唯一ID" />
        </el-form-item>
        <el-form-item label="名称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="头像" prop="avatarUrl">
          <el-input v-model="form.avatarUrl" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="语言" prop="language">
          <el-input v-model="form.language" placeholder="请输入语言" />
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="国家" prop="country">
          <el-input v-model="form.country" placeholder="请输入国家" />
        </el-form-item>
        <el-form-item label="金币" prop="gold">
          <el-input v-model="form.gold" placeholder="请输入金币" />
        </el-form-item>
        <el-form-item label="积分" prop="integral">
          <el-input v-model="form.integral" placeholder="请输入积分" />
        </el-form-item>
        <el-form-item label="等级" prop="level">
          <el-input v-model="form.level" placeholder="请输入等级" />
        </el-form-item>
        <el-form-item label="经验" prop="exp">
          <el-input v-model="form.exp" placeholder="请输入经验" />
        </el-form-item>
        <el-form-item label="水印id" prop="wId">
          <el-input v-model="form.wId" placeholder="请输入水印id" />
        </el-form-item>
        <el-form-item label="注册ip" prop="registerIp">
          <el-input v-model="form.registerIp" placeholder="请输入注册ip" />
        </el-form-item>
        <el-form-item label="注册时间" prop="registerTime">
          <el-date-picker clearable
            v-model="form.registerTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择注册时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="登录时间" prop="loginTime">
          <el-date-picker clearable
            v-model="form.loginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择登录时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="登录ip" prop="loginIp">
          <el-input v-model="form.loginIp" placeholder="请输入登录ip" />
        </el-form-item>
        <el-form-item label="是否删除" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入是否删除" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUsers, getUsers, delUsers, addUsers, updateUsers } from "@/api/metaverse-users/users";

export default {
  name: "Users",
  dicts: ['sys_show_hide', 'sys_normal_disable'],
  data() {
    return {
      drawer: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      usersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openId: null,
        nickName: null,
        gender: null,
        avatarUrl: null,
        city: null,
        language: null,
        province: null,
        country: null,
        gold: null,
        integral: null,
        level: null,
        exp: null,
        wId: null,
        registerIp: null,
        registerTime: null,
        loginTime: null,
        loginIp: null,
        isDel: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        openId: [
          { required: true, message: "小程序中的唯一ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUsers(this.queryParams).then(response => {
        this.usersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        openId: null,

        nickName: null,
        gender: null,
        avatarUrl: null,
        city: null,
        language: null,
        province: null,
        country: null,
        gold: null,
        integral: null,
        level: null,
        exp: null,
        wId: null,
        registerIp: null,
        registerTime: null,
        loginTime: null,
        loginIp: null,
        isDel: null,
        status: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUsers(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUsers(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUsers(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户编号为"' + ids + '"的数据项？').then(function() {
        return delUsers(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('metaverse-users/users/export', {
        ...this.queryParams
      }, `users_${new Date().getTime()}.xlsx`)
    },
    sel(row){
      const id = row.id || this.ids
      this.drawer = true;
      const 用户详情 = this.title = '用户详情';
      getUsers(id).then(resp => {
        console.log("详细信息接口返回：", resp)
        this.form = resp.data;
      })
    },

  }
};
</script>
<style>
.text_style {
  color: gray;
  padding-left: 20px;
  font-size: 14px;
  line-height: 45px;
}
</style>
